package com.ruoyi.web.controller.tool;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StringDate {
	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回月份
	 * 
	 * @return dateStr
	 */
	public static String getYear(String dateStr) {
		String year = "";
		if (dateStr != null && dateStr.length() > 4) {
			year = dateStr.substring(0, 4);

		}
		return year;
	}

	/**
	 * 根据参数获取当前时间x天以前的日期
	 * @param x
	 * @return
	 */
	public static String getXDayAgeDate(int x){
		String dateStr="";
		Calendar calendar = Calendar.getInstance();
		int day=calendar.get(Calendar.DAY_OF_YEAR);
		calendar.set(Calendar.DAY_OF_YEAR,day-x);
		Date date=calendar.getTime();
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		dateStr = df.format(date);
		return dateStr;
	}
	
	public static String getTime() {
		Calendar rightNow = Calendar.getInstance();
		Date date = rightNow.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = df.format(date);
		return now;
	}
	
	public static String getTime2() {
		Calendar rightNow = Calendar.getInstance();
		Date date = rightNow.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String now = df.format(date);
		return now;
	}
	public static String getTime3() {
		Calendar rightNow = Calendar.getInstance();
		Date date = rightNow.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String now = df.format(date);
		return now;
	}

	// 把时间格式化成YYYY-MM-dd hh24:mi时间格式字符串
	public static String getTime(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String now = df.format(date);
		return now;
	}
	
	// 把时间格式化成YYYY-MM-dd hh24:mi时间格式字符串
		public static String getTime4(Date date) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String now = df.format(date);
			return now;
		}
		
		public static String getYYYYMMddHHmissSSS(Date date) {
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSSSSSSSS");
			String now = df.format(date);
			return now;
		}

	// 把时间格式化成YYYY-MM-dd格式字符串
	public static String getYYYYMMdd(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String now = df.format(date);
		return now;
	}
//	 把时间YYYY-MM-dd hh:mi格式化成YYYY-MM-dd格式字符串
	public static String getYYYY_MM_dd(String dateStr) {
		String newDateStr = "";
		if (dateStr != null && dateStr.length() > 10) {
			newDateStr = dateStr.substring(0, 10);

		}
		return newDateStr;
	}
//	 把时间格式化成YYYY-MM-dd格式字符串
	public static String getYYYYMMdd2(Date date) {
		DateFormat df = new SimpleDateFormat("MM月dd日");
		String now = df.format(date);
		return now;
	}
	public static String getYYYYMMdd2(String date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		if(date.length()<13)df = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat df2 = new SimpleDateFormat("M月d日");
		String now="";
		try {
			now = df2.format(df.parse(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return now;
	}
	/**
	 * 把时间对象转换成xxxx年xx月xx日字符串格式
	 * @param date
	 * @return
	 */
	public static String getYYYYMMdd3(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		String now = df.format(date);
		return now;
	}
//	 把时间格式化成YYYY-MM格式字符串
	public static String getYYYYMM(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM");
		String now = df.format(date);
		return now;
	}
	
//	 把时间格式化成YYYY-MM格式字符串
	public static String getYYYY(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy");
		String now = df.format(date);
		return now;
	}

	// 把时间格式化成YYYYMMdd格式字符串
	public static String getYYYYMMdd(String dateStr) {
		String newDateStr = "";
		if (dateStr != null && dateStr.length() > 10) {
			newDateStr = dateStr.substring(0, 10);

		}
		return newDateStr;
	}
//	 把时间格式化成YYYYMMddHHmiss格式字符串
	public static String getYYYYMMddHHmiss(Date date) {
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String now = df.format(date);
		return now;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回月份，如果月份值以0开头，自动去除
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getMonth(String dateStr) {
		String month = "";
		if (dateStr != null && dateStr.length() > 0) {
			month = dateStr.substring(5, 7);
			if (month.charAt(0) == '0') {
				month = month.substring(1, 2);
			}
		}
		return month;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回月份，如果月份值以0开头，保留
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getMonth2(String dateStr) {
		String month = "";
		if (dateStr != null && dateStr.length() > 0) {
			month = dateStr.substring(5, 7);
		}
		return month;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回日值，如果日值以0开头，自动去除
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getDay(String dateStr) {
		String day = "";
		if (dateStr != null && dateStr.length() > 0) {
			day = dateStr.substring(8, 10);
			if (day.charAt(0) == '0') {
				day = day.substring(1, 2);
			}
		}
		return day;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回日值，如果日值以0开头，保留
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getDay2(String dateStr) {
		String day = "";
		if (dateStr != null && dateStr.length() > 0) {
			day = dateStr.substring(8, 10);
		}
		return day;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回小时值，如果小时值以0开头，自动去除
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getHours(String dateStr) {
		String hours = "";
		if (dateStr != null && dateStr.length() > 0) {
			hours = dateStr.substring(11, 13);
			if (hours.charAt(0) == '0') {
				hours = hours.substring(1, 2);
			}
		}
		return hours;
	}

	/**
	 * 传入YYYY-MM-dd hh24:mi时间格式，返回分值，如果分值以0开头，自动去除
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getMins(String dateStr) {
		String mins = "";
		if (dateStr != null && dateStr.length() > 0) {
			mins = dateStr.substring(14, 16);
			if (mins.charAt(0) == '0') {
				mins = mins.substring(1, 2);
			}
		}
		return mins;
	}

	// 计算两个日期相减的天数，两个日期格式分类是“yyyy-MM-dd”
	// NMH
	public static long getCompareDate(String startDate, String endDate)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = formatter.parse(startDate);
		Date date2 = formatter.parse(endDate);
		long l = date2.getTime() - date1.getTime();
		long d = l / (24 * 60 * 60 * 1000);
		return d;
	}
	
	// 计算两个日期相减的小时数，两个日期格式分类是“yyyy-MM-dd”
	// NMH
	public static long getCompareDateHour(String startDate, String endDate)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = formatter.parse(startDate);
		Date date2 = formatter.parse(endDate);
		long l = date2.getTime() - date1.getTime();
		long d = l / (60 * 60 * 1000);
		return d;
	}
	
	// 计算两个日期相减的分钟数，两个日期格式分类是“yyyy-MM-dd”
		// NMH
		public static long getCompareDateMinu(String startDate, String endDate)
				throws ParseException {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = formatter.parse(startDate);
			Date date2 = formatter.parse(endDate);
			long l = date2.getTime() - date1.getTime();
			long d = l / ( 60 * 1000);
			return d;
		}
		

	// 根据输入日期来获取此日期的前一天
	public static String getLastDayInfo(String nowDate) {
		String yesterday = "";
		int year = 0;
		int month = 0;
		int day = 0;
		try {
			// 2007-09-01
			year = Integer.parseInt(nowDate.substring(0, nowDate.indexOf("-")));
			month = Integer.parseInt(nowDate.substring(
					nowDate.indexOf("-") + 1, nowDate.lastIndexOf("-")));
			day = Integer.parseInt(nowDate
					.substring(nowDate.lastIndexOf("-") + 1));
			day = day - 1;
			if (day == 0) {
				month = month - 1;
				if (month == 0) {
					// January
					month = 12;
					day = 31;
					year = year - 1;
				} else {
					// not Jan.
					switch (month) {
					// 1|3|5|7|8|10|12) day=31;;
					case 1:
						day = 31;
						break;
					case 3:
						day = 31;
						break;
					case 5:
						day = 31;
						break;
					case 7:
						day = 31;
						break;
					case 8:
						day = 31;
						break;
					case 10:
						day = 31;
						break;
					case 12:
						day = 31;
						break;
					// 4|6|9|11) day=30;;
					case 4:
						day = 30;
						break;
					case 6:
						day = 30;
						break;
					case 9:
						day = 30;
						break;
					case 11:
						day = 30;
						break;
					case 2:
						if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
							// leap year
							day = 29;
						} else
							day = 28;

					}

				}
			}

			String monthStr = "";
			String dayStr = "";

			if (month < 10) {
				monthStr = "0" + String.valueOf(month);
			} else {
				monthStr = String.valueOf(month);
			}

			if (day < 10) {
				dayStr = "0" + String.valueOf(day);
			} else {
				dayStr = String.valueOf(day);
			}

			yesterday = String.valueOf(year) + "-" + monthStr + "-" + dayStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return yesterday;
	}

	// 根据当天来获取明天的日期
	public static String getNextDayInfo(String nowDate) {
		String tomorrowDay = "";
		int year = 0;
		int month = 0;
		int day = 0;
		try {
			year = Integer.parseInt(nowDate.substring(0, nowDate.indexOf("-")));
			month = Integer.parseInt(nowDate.substring(
					nowDate.indexOf("-") + 1, nowDate.lastIndexOf("-")));
			day = Integer.parseInt(nowDate
					.substring(nowDate.lastIndexOf("-") + 1));
			int sday = 0;
			switch (month) {
			// 1|3|5|7|8|10|12) day=31;;
			case 1:
				sday = 31;
				break;
			case 3:
				sday = 31;
				break;
			case 5:
				sday = 31;
				break;
			case 7:
				sday = 31;
				break;
			case 8:
				sday = 31;
				break;
			case 10:
				sday = 31;
				break;
			case 12:
				sday = 31;
				break;
			// 4|6|9|11) day=30;;
			case 4:
				sday = 30;
				break;
			case 6:
				sday = 30;
				break;
			case 9:
				sday = 30;
				break;
			case 11:
				sday = 30;
				break;
			case 2:
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					// leap year
					sday = 29;
				} else
					sday = 28;

			}
			day = day + 1;
			if (day > sday) {
				month = month + 1;
				day = 1;
				if (month > 12) {
					// January
					year = year + 1;
					month = 1;
				} else {
				}
			}

			String monthStr = "";
			String dayStr = "";

			if (month < 10) {
				monthStr = "0" + String.valueOf(month);
			} else {
				monthStr = String.valueOf(month);
			}

			if (day < 10) {
				dayStr = "0" + String.valueOf(day);
			} else {
				dayStr = String.valueOf(day);
			}

			tomorrowDay = String.valueOf(year) + "-" + monthStr + "-" + dayStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tomorrowDay;
	}

	// 根据所输入年月来判断当月天
	public static String[] daysOfTheMonth(String nowDate) {
		int year = 0;
		int month = 0;
		int day = 0;
		String days[] = null;
		try {
			// 2007-09-01
			// year =
			// Integer.parseInt(nowDate.substring(0,nowDate.indexOf("-")));
			month = Integer.parseInt(nowDate
					.substring(nowDate.indexOf("-") + 1));
			// not Jan.
			switch (month) {
			// 1|3|5|7|8|10|12) day=31;;
			case 1:
				day = 31;
				break;
			case 3:
				day = 31;
				break;
			case 5:
				day = 31;
				break;
			case 7:
				day = 31;
				break;
			case 8:
				day = 31;
				break;
			case 10:
				day = 31;
				break;
			case 12:
				day = 31;
				break;
			// 4|6|9|11) day=30;;
			case 4:
				day = 30;
				break;
			case 6:
				day = 30;
				break;
			case 9:
				day = 30;
				break;
			case 11:
				day = 30;
				break;
			case 2:
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					// leap year
					day = 29;
				} else
					day = 28;

			}
			days = new String[day];
			for (int i = 1; i <= day; i++) {

				if (i < 10) {
					days[i - 1] = nowDate + "-" + "0" + String.valueOf(i);
				} else {
					days[i - 1] = nowDate + "-" + String.valueOf(i);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return days;
	}
//	 根据输入日期来获取此日期的前一个月份YYYY-MM
	public static String getLastMonth(String nowDate) {
		String lastMonth = "";
		int year = 0;
		int month = 0;		
		try {
			// 2007-09-01
			year = Integer.parseInt(nowDate.substring(0, nowDate.indexOf("-")));
			month = Integer.parseInt(nowDate.substring(
					nowDate.indexOf("-") + 1));			
			month = month - 1;
			if (month == 0) {					
				month = 12;					
				year = year - 1;
			} 
			String monthStr = "";			
			if (month < 10) {
				monthStr = "0" + String.valueOf(month);
			} else {
				monthStr = String.valueOf(month);
			}
			lastMonth = String.valueOf(year) + "-" + monthStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lastMonth;
	}
//	 根据输入时间的小时分来获取下一个小时分HH-MM
	public static String getNextHhMm(String currSj) {
		String nextSj = "";
		int hour = 0;
		int minuter = 0;		
		try {
			// 23:59
			hour = Integer.parseInt(currSj.substring(0, currSj.indexOf(":")));
			minuter = Integer.parseInt(currSj.substring(
					currSj.indexOf(":") + 1));			
			minuter = minuter + 1;
			if (minuter == 60) {					
				minuter = 0;					
				hour = hour + 1;
			}
			if(hour == 24)
				hour=0;						
			if (hour < 10) {
				nextSj = "0" + String.valueOf(hour);
			} else {
				nextSj = String.valueOf(hour);
			}
			nextSj +=":";
			if (minuter < 10) {
				nextSj += "0" + String.valueOf(minuter);
			} else {
				nextSj += String.valueOf(minuter);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nextSj;
	}
	 /**
     * 获取某月的最后一天
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
         
        return lastDayOfMonth;
    }
    /**
     * 获取某月的第一天
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getFirstDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
         
        return lastDayOfMonth;
    }
    
    public static String getCurrentYearStartDay(){
    	return getTime2().substring(0,4)+"-01-01 00:00";
    }
    
 // 把时间格式化成YYYY-MM-dd hh24:mi时间格式字符串
	public static String getLastMonthTime() {
		Calendar c =Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String now = df.format(c.getTime());
		return now;
	}

	/**
	 * 根据传入的时间字符串，获取上一年的同期时间
	 * 
	 * @param x
	 * @return
	 */
	public static String getLastYearDate(String dateStr) {
		String lastYearDateStr = "";

		String year = getYear(dateStr);
		String lastYear = new Integer((new Integer(year).intValue() - 1))
				.toString();
		lastYearDateStr = lastYear + dateStr.substring(4, dateStr.length());
		;
		return lastYearDateStr;
	}

	/**
	 * 根据传入的时间字符串，获取下一天的时间的开始时间
	 * 
	 * @param dateStr 2010-01-01
	 * @param startTimeType 0:一天的开始时间为00:00, 1:一天的开始时间为 18:00
	 * @return 2010-01-02 00:00
	 */
	public static String getNextDayStartTime(String dateStr, int startTimeType) {
		Calendar calendar = Calendar.getInstance();
		String reStr = "";
		try {
			int year = Integer.parseInt(dateStr.substring(0, 4));
			int month = Integer.parseInt(dateStr.substring(5, 7));
			int day = Integer.parseInt(dateStr.substring(8, 10));
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month - 1);
			calendar.set(Calendar.DATE, day);

			calendar.add(Calendar.DATE, 1);
			reStr = calendar.get(Calendar.YEAR) + "-"
					+ calendar.get(Calendar.MONTH)+1 + "-"
					+ calendar.get(Calendar.DATE);
			if(startTimeType == 0){
				return reStr+" 00:00";
			}else{
				return reStr+" 18:00";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 根据传入的时间字符串，获取下一天的时间的开始时间
	 * 
	 * @param dateStr 2010-01-01
	 * @param startTimeType 0:一天的开始时间为00:00, 1:一天的开始时间为 18:00
	 * @return 2010-01-02 00:00
	 */
	public static String getDayStartTime(String dateStr, int startTimeType) {
		Calendar calendar = Calendar.getInstance();
		String reStr = "";
		try {
			int year = Integer.parseInt(dateStr.substring(0, 4));
			int month = Integer.parseInt(dateStr.substring(5, 7));
			int day = Integer.parseInt(dateStr.substring(8, 10));
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month - 1);
			calendar.set(Calendar.DATE, day);
			reStr = calendar.get(Calendar.YEAR) + "-"
			+ calendar.get(Calendar.MONTH)+1 + "-"
			+ calendar.get(Calendar.DATE);
			if(startTimeType == 0){
				return reStr+" 00:00";
			}else{
				return reStr+" 18:00";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
